package client_controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

import client_controllers.CustomerReportsController.Back;
import client_gui.CampaignReportsGUI;
import client_gui.CustomerReportsGUI;
import client_logic.CcrmClient;
import client_logic.dbHandler;
import entity.Boss;

public class CampaignReportsController implements Serializable,dbHandler {

	private CampaignReportsController crc;
	private CampaignReportsGUI crg;
	private MarketingManagerController mmc;
	private EmployeeLoginController elc;
	private Boss b=new Boss(null,null,null);
	
	public CampaignReportsController(CampaignReportsGUI crg,EmployeeLoginController elc, MarketingManagerController mmc) {
		crc = this;   
		this.crg=crg;
		   this.mmc=mmc;
		   this.crg.getBtnBack().addActionListener(new Back());
		   this.crg.getBtnGetReports().addActionListener(new getCampaignsLis());
		   getCampaigns();

	}
	private void getCampaigns() {
		this.b.setCommand("getCampaigns");
		   this.b.setControl("CampaignReportsController");
		   this.Search();
		
	}
	public class Back implements ActionListener{
		public void actionPerformed(ActionEvent e) {
		crg.dispose();
		mmc.getMmg().setVisible(true);
		}
	}
	public class getCampaignsLis implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			  crc.b.setCommand("getCampaignReports");
			  crc.b.setControl("CampaignReportsController");
			  crc.b.setMsg(crg.getCaId().getSelectedItem().toString()); //this is id . -means it should be string 
			  System.out.println("before doing the query");
			  update();
		}
	}
	@Override
	public void Add() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void Search() {
		try {
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public String Delete() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void HandleMsg(Object msg) {
		this.setB((Boss) msg);
		if(this.getB().getCommand().equals("getCampaigns")){
			ArrayList <String> campaigns ;
			campaigns= (ArrayList<String>) this.getB().getObj();
			for(String i :campaigns)
				crg.getCaId().addItem(i); 
			}
		if(this.getB().getCommand().equals("getCampaignReports")){
			ArrayList<String> campaign = (ArrayList<String>) b.getObj();

			for(String i: campaign )
				crg.log.addElement(i);
		}
		
		
	}
	@Override
	public void update() {
		try {
			CcrmClient.inet.setCurrCon(this);
			CcrmClient.inet.sendToServer(b);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public CampaignReportsGUI getCrg() {
		return crg;
	}
	public MarketingManagerController getMmc() {
		return mmc;
	}
	public EmployeeLoginController getElc() {
		return elc;
	}
	public Boss getB() {
		return b;
	}
	public void setCrg(CampaignReportsGUI crg) {
		this.crg = crg;
	}
	public void setMmc(MarketingManagerController mmc) {
		this.mmc = mmc;
	}
	public void setElc(EmployeeLoginController elc) {
		this.elc = elc;
	}
	public void setB(Boss b) {
		this.b = b;
	}

}
